package com.example.demo.controllers;

import com.example.demo.form.LoginByAccountPassword;
import com.example.demo.form.LoginByEmailCode;
import com.example.demo.form.LoginByEmailPassword;
import com.example.demo.form.UserEnrollForm;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author luolinila
 */
@RestController
public class UserControllers {

    @Autowired
    private UserService userService;

    //发送邮箱验证码
    @PostMapping(value = "/user/email")
    public Result<String> email(@RequestParam(value = "EMAIL") String email) {
        System.out.println(email);
        if(!email.contains("@") || !email.contains(".com")) {
            return new Result<>(10004, "邮箱格式错误", null);
        }
        String str = userService.sendEmail(email);
        System.out.println(str);
        if(Objects.equals(str, "error")) {
            return new Result<>(10005, "发送失败", null);
        }
        return new Result<>(10006, "发送成功", null);
    }

    //注册用户
    @PostMapping(value = "/user/enroll")
    public Result<User> enroll(@RequestBody UserEnrollForm userEnrollForm) {
        System.out.println(userEnrollForm.getEmail());
        if(userEnrollForm.getUsername().length() < 8 || userEnrollForm.getUsername().length() > 12) {
            return new Result<>(10004, "用户名格式错误", null);
        }
        if(userEnrollForm.getPassword().length() < 8 || userEnrollForm.getPassword().length() > 12) {
            return new Result<>(10005, "密码格式错误", null);
        }
        if (!userEnrollForm.getEmail().contains("@") || !userEnrollForm.getEmail().contains(".com")) {
            return new Result<>(10006, "邮箱格式错误", null);
        }
        int n = userService.enroll(userEnrollForm.getUsername(), userEnrollForm.getPassword(), userEnrollForm.getEmail(), userEnrollForm.getState());
        if(n == -1) {
            return new Result<>(10007, "邮箱被注册", null);
        }
        if (n == -2) {
            return new Result<>(10008, "验证码过期或错误", null);
        }
        User user = userService.getUserById(n);
        if(user == null) {
            return new Result<>(10009, "注册失败", null);
        }
        return new Result<>(200, "注册成功", user);
    }

    //邮箱验证码登录
    @PostMapping(value = "/user/login/email/code")
    public Result<User> loginByEmailCode(@RequestBody LoginByEmailCode loginByEmailCode) {
        if (!loginByEmailCode.getEmail().contains("@") || !loginByEmailCode.getEmail().contains(".com")) {
            return new Result<>(10006, "邮箱格式错误", null);
        }
        User user = userService.loginByEmailCode(loginByEmailCode.getEmail(), loginByEmailCode.getCode());
        if(user == null) {
            return new Result<>(10007, "验证码过期或错误", null);
        }
        return new Result<>(200, "登录成功", user);
    }

    //邮箱密码登录
    @PostMapping(value = "/user/login/email/password")
    public Result<User> loginByEmailPassword(@RequestBody LoginByEmailPassword loginByEmailPassword) {
        if (!loginByEmailPassword.getEmail().contains("@") || !loginByEmailPassword.getEmail().contains(".com")) {
            return new Result<>(10006, "邮箱格式错误", null);
        }
        User user = userService.loginByEmailPassword(loginByEmailPassword.getEmail(), loginByEmailPassword.getPassword());
        if(user == null) {
            return new Result<>(10004, "账号密码错误", null);
        }
        return new Result<>(200, "登录成功", user);
    }

    //账号密码登录
    @PostMapping(value = "/user/login/account/password")
    public Result<User> loginByAccountPassword(@RequestBody LoginByAccountPassword loginByAccountPassword) {
        User user = userService.loginByAccountPassword(loginByAccountPassword.getAccount(), loginByAccountPassword.getPassword());
        if (user == null) {
            return new Result<>(10004, "账号密码错误", null);
        }
        return new Result<>(200, "登录成功", user);
    }
}
